﻿using Azylee.Core.IOUtils.TxtUtils;

namespace JMBox.Config
{
    /// <summary>
    /// 配置信息
    /// </summary>
    public static partial class R
    {
        public class GlobalConfig
        {
            //标头名称
            public static string SHConfigHeader = "SHConfigHeader";
            //Wifi信息
            public static string WifiNameStr = "WifiName";
            public static string WifiName = "MGiXN";
            public static string WifiPwdStr = "WifiPwd";
            public static string WifiPwd = "wmg1ovesxn.";
            //清理热键
            public static string ClearKeyStr = "ClearKey";
            public static string ClearKey = " ";
            //清理触发键
            public static string ClearTriggerKeyStr = "ClearTriggerKey";
            public static string ClearTriggerKey = "R";
            //清理停止键
            public static string ClearStopKeyStr = "ClearStopKey";
            public static string ClearStopKey = "T";
            //持续次数(基数,单位秒)
            public static string ClearDurationStr = "ClearDuration";
            public static int ClearDuration = 99999;
            //随机数上限、下限
            public static string ClearRandomMaxStr = "ClearRandomMax";
            public static int ClearRandomMax = 40;
            public static string ClearRandomMinStr = "ClearRandomMin";
            public static int ClearRandomMin = 10;
            //主偏移量
            public static string MIME_OBJ_OFFSET_X_Str = "MIME_OBJ_OFFSET_X";
            public static int MIME_OBJ_OFFSET_X = 0;
            public static string MIME_OBJ_OFFSET_Y_Str = "MIME_OBJ_OFFSET_Y";
            public static int MIME_OBJ_OFFSET_Y = 0;
            //目标偏移量
            public static string TARGET_OBJ_OFFSET_X_Str = "TARGET_OBJ_OFFSET_X";
            public static int TARGET_OBJ_OFFSET_X = 0;
            public static string TARGET_OBJ_OFFSET_Y_Str = "TARGET_OBJ_OFFSET_Y";
            public static int TARGET_OBJ_OFFSET_Y = 0;
            //开启持续LOCK触发键
            public static string LockTriggerKeyStr = "LockTriggerKey";
            public static string LockTriggerKey = "4";
            //总开关触发键
            public static string AllFunTriggerKeyStr = "AllFunTriggerKey";
            public static string AllFunTriggerKey = "F8";
            //匹配度
            public static string MatchThresholdStr = "MatchThreshold";
            public static double MatchThreshold = 0.93;

            //移动偏差
            public static string User_Move_OffsetStr = "User_Move_Offset";
            public static int User_Move_Offset = 60;
            //单步距离
            public static string User_Move_One_StepStr = "User_Move_One_Step";
            public static int User_Move_One_Step = 90;
            //中心位置
            public static string CENTER_POI_X_Str = "CENTER_POI_X";
            public static int CENTER_POI_X = 0;
            public static string CENTER_POI_Y_Str = "CENTER_POI_Y";
            public static int CENTER_POI_Y = 0;

            public static string SECRET_KEY_STR = "SECRET_KEY";
            public static string SECRET_KEY = "";
            //有效期
            public static string ALIVE_DATE_TIME_STR = "2024-02-20 12:00:00";
            public static DateTime ALIVE_DATE_TIME = Convert.ToDateTime(ALIVE_DATE_TIME_STR);
            public static void LoadConfig()
            {
                try
                {
                    InitSecretKey();
                    InitCenterPoiConfig();
                    InitUserMoveConfig();
                    InitWifiConfig();
                    InitLockConfig();
                    InitMatchThresholdConfig();
                    InitAllFunConfig();
                    InitClearConfig();
                    InitPoiOffsetConfig();
                    Contact.MainUI.UpdateFunctionDesc();
                }
                catch (Exception ex)
                {
                    MessageBox.Show("配置加载失败:" + ex.Message);
                }

            }

            public static void InitSecretKey()
            {
                string IniSecretKey = IniTool.GetString(R.Contact.Settings, SHConfigHeader, SECRET_KEY_STR, "");
                SECRET_KEY = IniSecretKey;
            }

            private static void InitCenterPoiConfig()
            {
                //没有则初始化，有则加载
                string IniCENTER_POI_X = IniTool.GetString(R.Contact.Settings, SHConfigHeader, CENTER_POI_X_Str, "");
                string IniCENTER_POI_Y = IniTool.GetString(R.Contact.Settings, SHConfigHeader, CENTER_POI_Y_Str, "");
                if (String.IsNullOrEmpty(IniCENTER_POI_X))
                {
                    IniTool.Set(R.Contact.Settings, SHConfigHeader, CENTER_POI_X_Str, CENTER_POI_X.ToString());
                    IniTool.Set(R.Contact.Settings, SHConfigHeader, CENTER_POI_Y_Str, CENTER_POI_Y.ToString());
                }
                else
                {
                    CENTER_POI_X = int.Parse(IniCENTER_POI_X);
                    CENTER_POI_Y = int.Parse(IniCENTER_POI_Y);
                }
            }

            private static void InitUserMoveConfig()
            {
                //没有则初始化，有则加载
                string IniUser_Move_Offset = IniTool.GetString(R.Contact.Settings, SHConfigHeader, User_Move_OffsetStr, "");
                string IniUser_Move_One_Step = IniTool.GetString(R.Contact.Settings, SHConfigHeader, User_Move_One_StepStr, "");
                if (String.IsNullOrEmpty(IniUser_Move_Offset))
                {
                    IniTool.Set(R.Contact.Settings, SHConfigHeader, User_Move_OffsetStr, User_Move_Offset.ToString());
                    IniTool.Set(R.Contact.Settings, SHConfigHeader, User_Move_One_StepStr, User_Move_One_Step.ToString());
                }
                else
                {
                    User_Move_Offset = int.Parse(IniUser_Move_Offset);
                    User_Move_One_Step = int.Parse(IniUser_Move_One_Step);
                }
            }

            private static void InitMatchThresholdConfig()
            {
                //没有则初始化，有则加载
                string IniMatchThreshold = IniTool.GetString(R.Contact.Settings, SHConfigHeader, MatchThresholdStr, "");
                if (String.IsNullOrEmpty(IniMatchThreshold))
                {
                    IniTool.Set(R.Contact.Settings, SHConfigHeader, MatchThresholdStr, MatchThreshold.ToString());
                }
                else
                {
                    MatchThreshold = double.Parse(IniMatchThreshold);
                }
            }

            private static void InitAllFunConfig()
            {
                //没有则初始化，有则加载
                string IniAllFunTriggerKey = IniTool.GetString(R.Contact.Settings, SHConfigHeader, AllFunTriggerKeyStr, "");
                if (String.IsNullOrEmpty(IniAllFunTriggerKey))
                {
                    IniTool.Set(R.Contact.Settings, SHConfigHeader, AllFunTriggerKeyStr, AllFunTriggerKey);
                }
                else
                {
                    AllFunTriggerKey = IniAllFunTriggerKey;
                }
            }
            private static void InitLockConfig()
            {
                //没有则初始化，有则加载
                string IniLockTriggerKey = IniTool.GetString(R.Contact.Settings, SHConfigHeader, LockTriggerKeyStr, "");
                if (String.IsNullOrEmpty(IniLockTriggerKey))
                {
                    IniTool.Set(R.Contact.Settings, SHConfigHeader, LockTriggerKeyStr, LockTriggerKey);
                }
                else
                {
                    LockTriggerKey = IniLockTriggerKey;
                }
            }


            /// <summary>
            /// 初始化WIFI配置
            /// </summary>
            private static void InitWifiConfig()
            {
                //初始化WIFI配置，没有则初始化，有则加载
                string IniWifiName = IniTool.GetString(R.Contact.Settings, SHConfigHeader, WifiNameStr, "");
                string IniWifiPwd = IniTool.GetString(R.Contact.Settings, SHConfigHeader, WifiPwdStr, "");
                if (String.IsNullOrEmpty(IniWifiName))
                {
                    IniTool.Set(R.Contact.Settings, SHConfigHeader, WifiNameStr, WifiName);
                    IniTool.Set(R.Contact.Settings, SHConfigHeader, WifiPwdStr, WifiPwd);
                }
                else
                {
                    WifiName = IniWifiName;
                    WifiPwd = IniWifiPwd;
                }
            }

            /// <summary>
            /// 初始化清理配置
            /// </summary>
            private static void InitClearConfig()
            {
                //初始化Clear配置，没有则初始化，有则加载
                string IniClearKey = IniTool.GetString(R.Contact.Settings, SHConfigHeader, ClearKeyStr, "");
                string IniClearTrigger = IniTool.GetString(R.Contact.Settings, SHConfigHeader, ClearTriggerKeyStr, "");
                string IniClearStop = IniTool.GetString(R.Contact.Settings, SHConfigHeader, ClearStopKeyStr, "");
                string IniClearDuration = IniTool.GetString(R.Contact.Settings, SHConfigHeader, ClearDurationStr, "");
                string IniClearRandomMin = IniTool.GetString(R.Contact.Settings, SHConfigHeader, ClearRandomMinStr, "");
                string IniClearRandomMax = IniTool.GetString(R.Contact.Settings, SHConfigHeader, ClearRandomMaxStr, "");
                //1
                if (String.IsNullOrEmpty(IniClearKey))
                {
                    IniTool.Set(R.Contact.Settings, SHConfigHeader, ClearKeyStr, ClearKey);
                }
                else
                {
                    ClearKey = IniClearKey;
                }

                //2
                if (String.IsNullOrEmpty(IniClearTrigger))
                {
                    IniTool.Set(R.Contact.Settings, SHConfigHeader, ClearTriggerKeyStr, ClearTriggerKey);
                }
                else
                {
                    ClearTriggerKey = IniClearTrigger;
                }

                //3
                if (String.IsNullOrEmpty(IniClearStop))
                {
                    IniTool.Set(R.Contact.Settings, SHConfigHeader, ClearStopKeyStr, ClearStopKey);
                }
                else
                {
                    ClearStopKey = IniClearStop;
                }

                //4
                if (String.IsNullOrEmpty(IniClearDuration))
                {
                    IniTool.Set(R.Contact.Settings, SHConfigHeader, ClearDurationStr, ClearDuration);
                }
                else
                {
                    ClearDuration = int.Parse(IniClearDuration);
                }

                //5
                if (String.IsNullOrEmpty(IniClearRandomMin))
                {
                    IniTool.Set(R.Contact.Settings, SHConfigHeader, ClearRandomMinStr, ClearRandomMin);
                }
                else
                {
                    ClearRandomMin = int.Parse(IniClearRandomMin);
                }

                //6
                if (String.IsNullOrEmpty(IniClearRandomMax))
                {
                    IniTool.Set(R.Contact.Settings, SHConfigHeader, ClearRandomMaxStr, ClearRandomMax);
                }
                else
                {
                    ClearRandomMax = int.Parse(IniClearRandomMax);
                }
            }


            private static void InitPoiOffsetConfig()
            {
                //没有则初始化，有则加载
                string Ini_MIME_OBJ_OFFSET_X = IniTool.GetString(R.Contact.Settings, SHConfigHeader, MIME_OBJ_OFFSET_X_Str, "");
                string Ini_MIME_OBJ_OFFSET_Y = IniTool.GetString(R.Contact.Settings, SHConfigHeader, MIME_OBJ_OFFSET_Y_Str, "");
                string Ini_TARGET_OBJ_OFFSET_X = IniTool.GetString(R.Contact.Settings, SHConfigHeader, TARGET_OBJ_OFFSET_X_Str, "");
                string Ini_TARGET_OBJ_OFFSET_Y = IniTool.GetString(R.Contact.Settings, SHConfigHeader, TARGET_OBJ_OFFSET_Y_Str, "");


                if (String.IsNullOrEmpty(Ini_MIME_OBJ_OFFSET_X))
                {
                    IniTool.Set(R.Contact.Settings, SHConfigHeader, MIME_OBJ_OFFSET_X_Str, MIME_OBJ_OFFSET_X);
                }
                else
                {
                    MIME_OBJ_OFFSET_X = int.Parse(Ini_MIME_OBJ_OFFSET_X);
                }

                if (String.IsNullOrEmpty(Ini_MIME_OBJ_OFFSET_Y))
                {
                    IniTool.Set(R.Contact.Settings, SHConfigHeader, MIME_OBJ_OFFSET_Y_Str, MIME_OBJ_OFFSET_Y);
                }
                else
                {
                    MIME_OBJ_OFFSET_Y = int.Parse(Ini_MIME_OBJ_OFFSET_Y);
                }

                if (String.IsNullOrEmpty(Ini_TARGET_OBJ_OFFSET_X))
                {
                    IniTool.Set(R.Contact.Settings, SHConfigHeader, TARGET_OBJ_OFFSET_X_Str, TARGET_OBJ_OFFSET_X);
                }
                else
                {
                    TARGET_OBJ_OFFSET_X = int.Parse(Ini_TARGET_OBJ_OFFSET_X);
                }

                if (String.IsNullOrEmpty(Ini_TARGET_OBJ_OFFSET_Y))
                {
                    IniTool.Set(R.Contact.Settings, SHConfigHeader, TARGET_OBJ_OFFSET_Y_Str, TARGET_OBJ_OFFSET_Y);
                }
                else
                {
                    TARGET_OBJ_OFFSET_Y = int.Parse(Ini_TARGET_OBJ_OFFSET_Y);
                }
            }

            public static bool IsAlive()
            {
                return DateTime.Now < ALIVE_DATE_TIME;
            }

        }
        
    }

}
